﻿/* ************************************************************************************* 
 * CREATED BY:          GIAPCM
 * CREATED DATE:        10/02/2009
 * PROJECT:             MC ENT WEBSITE PROJECT
 * PURPOSE:             
 * MODIFIED BY:         None
 * MODIFIED DATE:       None
 * MODIFIED REASON:     None
 * NOTES:               None
 **************************************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MCWEBSITE.BUL;
using MCWEBSITE.ENTITY;
using System.Collections.Generic;

public partial class Modules_WebsiteBackEnd_wucResourceTypeDetail : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    #region "BINDING DATA CONTROLS"

    protected void BindDataDetail(int ItemID)
    {
        this.BindLanguage();
        if (ItemID != -1)
        {
            websiteResourceTypeInfo objResourceType = websiteResourceTypeBUL.Load(ItemID);
            lblResourceTypeID.Text = Convert.ToString(objResourceType.ResourceTypeID);
            tbResourceTypeCode.Text = objResourceType.ResourceTypeCode;
            tbTypeName.Text = objResourceType.TypeName;
            tbDescription.Text = objResourceType.Description;
            string Language = objResourceType.LanguageCode.ToString();
            this.rcbLanguage.Items.FindItemByValue(Language).Selected = true;
        }
        else
        {
            lblResourceTypeID.Text = "";
            tbResourceTypeCode.Text = "";
            tbTypeName.Text = "";
            tbDescription.Text = "";
        }
    }

    protected void BindLanguage()
    {
        List<websiteLanguageInfo> lstwebsiteAdZoneInfo = new List<websiteLanguageInfo>();
        lstwebsiteAdZoneInfo = websiteLanguageBUL.GetAll();
        this.rcbLanguage.DataSource = lstwebsiteAdZoneInfo;
        this.rcbLanguage.DataTextField = "languageName";
        this.rcbLanguage.DataValueField = "LanguageCode";
        this.rcbLanguage.DataBind();
    }

    #endregion

    #region "DATA HANDLING PROCEDURE/FUNCTION"

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    protected int InsertRow()
    {
        websiteResourceTypeInfo objResourceType = new websiteResourceTypeInfo();
        objResourceType.ResourceTypeID = 0;
        objResourceType.ResourceTypeCode = tbResourceTypeCode.Text.Trim();
        objResourceType.TypeName = tbTypeName.Text.Trim();
        objResourceType.Description = tbDescription.Text.Trim();
        objResourceType.LanguageCode = this.rcbLanguage.SelectedValue;
        return websiteResourceTypeBUL.Save(objResourceType);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    protected int DeleteRow()
    {
        return websiteResourceTypeBUL.Remove(Convert.ToInt32(lblResourceTypeID.Text.Trim()));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    protected int UpdateRow()
    {
        websiteResourceTypeInfo objResourceType = new websiteResourceTypeInfo();
        objResourceType.ResourceTypeID = Convert.ToInt32(lblResourceTypeID.Text.Trim());
        objResourceType.ResourceTypeCode = tbResourceTypeCode.Text.Trim();
        objResourceType.TypeName = tbTypeName.Text.Trim();
        objResourceType.Description = tbDescription.Text.Trim();
        objResourceType.LanguageCode = this.rcbLanguage.SelectedValue;
        return websiteResourceTypeBUL.Save(objResourceType);
    }

    #endregion

    #region "CONTROL EVENT HANDLER"


    #endregion

    #region "PUBLIC PROCEDURE/FUNCTION"
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ClickedButton"></param>
    /// <returns></returns>   
    public string PerformToolbarEvent(ToolbarButton ClickedButton)
    {
        string ResultString = "";
        int ReturnResult;

        if (lblCurrentMode.Text.ToString().ToUpper() == ToolbarStatus.NEW.ToString().ToUpper() &&
            ClickedButton == ToolbarButton.SAVEBUTTON)
        {
            if (Page.IsValid)
            {
                ReturnResult = this.InsertRow();
                if ((int)ReturnResult > 0) ResultString = "Bản ghi được bổ xung thành công";
                else ResultString = "Lỗi trong quá trình bổ xung bản ghi";
            }
        }

        if (lblCurrentMode.Text.ToString().ToUpper() == ToolbarStatus.EDIT.ToString().ToUpper() &&
            ClickedButton == ToolbarButton.SAVEBUTTON)
        {
            if (Page.IsValid)
            {
                ReturnResult = this.UpdateRow();
                if ((int)ReturnResult > 0) ResultString = "Bản ghi được cập nhật thành công";
                else ResultString = "Lỗi trong quá trình cập nhật bản ghi";
            }
        }

        if (lblCurrentMode.Text.ToString().ToUpper() == ToolbarStatus.EDIT.ToString().ToUpper() &&
            ClickedButton == ToolbarButton.DELBUTTON)
        {
            ReturnResult = this.DeleteRow();
            if ((int)ReturnResult > 0) ResultString = "Bản ghi được xóa thành công";
            else ResultString = "Lỗi trong quá trình xóa bản ghi";
        }

        return ResultString;
    }


    public void SetControlMode(ToolbarStatus ToolbarMode)
    {
        if (ToolbarMode == ToolbarStatus.NEW)
        {
            lblDetailMode.Text = "TẠO MỚI BẢN GHI";
            lblCurrentMode.Text = ToolbarStatus.NEW.ToString().ToUpper();
        }
        if (ToolbarMode == ToolbarStatus.EDIT)
        {
            lblDetailMode.Text = "CHỈNH SỬA BẢN GHI";
            lblCurrentMode.Text = ToolbarStatus.EDIT.ToString().ToUpper();
        }
    }

    public void SetDetailData(int ItemID)
    {
        this.BindDataDetail(ItemID);
    }

    public void RefreshDetailView()
    {
        if (lblCurrentMode.Text.ToString().ToUpper() == ToolbarStatus.NEW.ToString().ToUpper())
        {
            this.BindDataDetail(-1);
        }
        else
        {
            int SelectedItemID = Convert.ToInt32(lblResourceTypeID.Text.Trim());
            this.BindDataDetail(SelectedItemID);
        }
    }

    #endregion
}
